// var createError = require('http-errors');
// var express = require('express');
var path = require('path');
// var cookieParser = require('cookie-parser');
// var logger = require('morgan');

// var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
import express from 'express'
import bodyParser from 'body-parser'
var app = express();
import config from './config/index'
import cookie from 'cookie-parser'
import session from 'express-session'
// import ioredis from 'ioredis'
// var ioRedis = require('ioredis');
// var redis = new ioRedis();

// import connectMongo from 'connect-mongo'
import db from './mongodb/index'
import connectRedis from 'connect-redis'
// var RedisStore = connectRedis(session)
import router from './routes/index'
import dtime from 'time-formater'
console.log(dtime().format('YYYY-MM-DD HH:mm:ss'))
// const MongoStore = connectMongo(session)
app.all('*', (req, res, next) => {
    const {
        origin,
        Origin,
        referer,
        Referer
    } = req.headers
    const allowOrigin = origin || Origin || referer || Referer || '*'
    res.header('Access-Control-Allow-Origin', allowOrigin)
    res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization,x-Requested-with')
    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
    res.header('Access-Control-Allow-Credentials', true) // 可以带cookies
    res.header('X-Powered-By', 'Express')
    if (req.method === 'OPTIONS') {
        res.sendStatus(200)
    } else {
        next()
    }
})
// view engine setup
// app.set('views', path.join(__dirname, 'views'));
// app.set('view engine', 'jade');
// console.log(router)
// app.use(logger('dev'));
app.use(cookie());
app.use(bodyParser.json());
// app.set('trust proxy', 1);// trust first proxy
// app.use(session({
//     secret: 'keyboard cat',
//     resave: false,
//     saveUninitialized: true,
//     cookie: { secure: true }
// }));
app.use("/img",express.static(path.join(__dirname, 'img')));
app.use(bodyParser.urlencoded({ extended: false }));
router(app);
// app.use(cookieParser());
// app.use(express.static(path.join(__dirname, 'public')));

// app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
// app.use(function(req, res, next) {
//   next(createError(404));
// });

// error handler
// app.use(function(err, req, res, next) {
//   // set locals, only providing error in development
//   res.locals.message = err.message;
//   res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
//   res.status(err.status || 500);
//   res.render('error');
// });
app.listen(config.port,()=>{
    console.log(`start${config.port}`)
})
module.exports = app;
